package org.jpass.dc.business.admin.dao;

import java.util.ArrayList;
import java.util.List;

import org.jpass.dc.business.admin.bean.AdminBean;
import org.jpass.dc.common.bean.Page;
import org.jpass.dc.common.dao.GenericDao;
import org.jpass.dc.common.util.RefParaValue;

public class AdminDao extends GenericDao<AdminBean> {

	public AdminDao() {
		super(AdminBean.class);
	}
	/**
	 * 创建管理员
	 * @param admin 管理员对象
	 * @return
	 */
	public void addAdmin(AdminBean admin) throws Exception {
		this.save(admin);
	}
	
	/**
	 * 修改管理员信息
	 * @param admin 管理员对象
	 * @return
	 */
	public void updateAdmin(AdminBean admin) throws Exception {
		this.update(admin);
	}

	/**
	 * 查询管理员
	 * @param name 管理员名称
	 * @return
	 */
	public AdminBean findAdminByName(String name) throws Exception{
		String hql="";
		hql="from AdminBean as admin where admin.name= '" + name + "'";
		AdminBean admin=(AdminBean) this.query(hql);
		return admin;
	}
	
	/**
	 * 查询管理员
	 * @param id 管理员di
	 * @return
	 */
	public AdminBean findAdminById(String id) throws Exception{
		String hql="";
		hql="from AdminBean as admin where admin.id= '" + id + "'";
		AdminBean admin=(AdminBean) this.query(hql);
		return admin;
	}
	
	/**
	 * 返回分页管理员列表
	 * @return
	 */
	public List<AdminBean> findAdminPaging(Page page) throws Exception {
		List<AdminBean> list=new ArrayList<AdminBean>();
		String countHql = "select count(*) from AdminBean admin ";
		String hql="from AdminBean admin";
		list = this.queryForList(countHql,hql,null,page);
		return list;
	}
	
	/**
	 * 返回分页管理员列表
	 * @return
	 */
	public List findBackAdminList(Page page, String findUsername)
	throws Exception {
		List list = new ArrayList();
		String countHql = "select count(*) from AdminBean admin ";
		StringBuffer searchHql = new StringBuffer(
				"select new Map(A.id as id, A.name as name, A.realName as realName, A.password as password, A.authority as authority,"
				+ " A.authorityName as authorityName, A.createdTime as createdTime, A.creater as creater, A.isOnline as isOnline, A.loginTimes as loginTimes,"
				+ " A.lastSignTime as lastSignTime, A.notes as notes)"
				+ " from AdminBean as A where A.authority>0 ");
		if(!RefParaValue.empty(findUsername)){
			String str = " and A.name like '%"+ findUsername +"%'";
			searchHql.append(str);
		}
		searchHql.append(" order by A.createdTime desc ");
		list = this.queryForList(countHql, searchHql.toString(), null, page);
		return list;
	}

	
	/**
	 * 查询所有管理员
	 * @return
	 */
	public List<AdminBean> findALLAdminList() throws Exception {
		List<AdminBean> list=new ArrayList<AdminBean>();
		String hql="from AdminBean admin";
		list = this.queryAllUserList(hql);
		return list;
	}
}
